package com.dhecp.project.api.service.impl;

import com.dhecp.project.api.domain.ApiLog;
import com.dhecp.project.api.mapper.ApiLogMapper;
import com.dhecp.project.api.service.ApiLogService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
import java.util.Map;
import java.util.UUID;

@Service
public class ApiLogServiceImpl implements ApiLogService {

    private static final Logger logger = LoggerFactory.getLogger(ApiLogServiceImpl.class);

    @Autowired
    private  ApiLogMapper apiLogMapper;

    @Override
    public List<ApiLog> queryApiLogList(Map<String, Object> params) {
        return apiLogMapper.findApiLogList(params);
    }

    @Override
    public ApiLog queryApiLogById(int id) {
        return apiLogMapper.findApiLogById(id);
    }

    @Override
    public int addApiLog(String system, String type, String input, String output, int code, String message) {
        int result = 0;
        try {
            String guid = UUID.randomUUID().toString().toUpperCase();
            ApiLog apiLog = new ApiLog();
            apiLog.setSystem(system);
            apiLog.setType(type);
            apiLog.setInput(input);
            apiLog.setOutput(output);
            apiLog.setCode(code);
            apiLog.setMessage(message);
            apiLogMapper.insert(apiLog);
            result = apiLog.getId();
        }catch (Exception ex){
            logger.error("调用API接口保存日志出错，错误信息为：" + ex.getMessage());
        }
        return result;
    }

    @Override
    public void updateApiLog(int id,String system, String type, String input, String output, int code, String message) {
        try{
            ApiLog apiLog = apiLogMapper.findApiLogById(id);
            if(apiLog != null){
                apiLog.setOutput(output);
                apiLog.setCode(code);
                apiLog.setMessage(message);
                apiLogMapper.update(apiLog);
            }else{
                addApiLog(system, type, input, output, code, message);
            }
        }catch (Exception ex){
            logger.error("调用API接口更新日志出错，错误信息为：" + ex.getMessage());
        }
    }
}
